package com.laworks.urlutils;


import java.io.IOException;
import java.io.InputStream;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

class UrlHandler
{
    public UrlHandler(String url)
    {
        this.url = url;
    }
    
    public HttpResponse requstResponse() throws Exception
    {
        httpGetRequest = new HttpGet(this.url);
        this.response = httpClient.execute(httpGetRequest);

        return this.response;
    }
    
    public StatusLine getStatusLine() throws Exception
    {
        if(this.response == null)
        {
            throw new Exception("the response is null!!!");
        }
        this.statusLine = this.response.getStatusLine();
        return this.statusLine;
    }
    
    
    public int getStatusCode() throws Exception
    {
        if(this.statusLine == null)
        {
            throw new Exception("the statusLine is null!!!");
        }
        return this.statusLine.getStatusCode();
    }
    
    public boolean isResponseOK() throws Exception
    {
        if(this.statusLine == null)
        {
            throw new Exception("the statusLine is null!!!");
        }
        return HttpStatus.SC_OK == this.statusLine.getStatusCode();
    }
    
    public String getContentType() throws Exception
    {
        if(this.response == null)
        {
            throw new Exception("the response is null!!!");
        }
        Header[] headers = this.response.getHeaders("Content-Type");
        
        String content = headers[0].toString();
        String [] contentArray = content.split(":");
        
        String contentType;
        try
        {
            contentType = contentArray[1].trim();
        }
        catch (ArrayIndexOutOfBoundsException e) {
            // TODO: handle exception
            return null;
        }
        return contentType;
    }
    
    public InputStream getResponseEntityAsStream() throws Exception
    {
        if(this.response == null)
        {
            throw new Exception("the response is null!!!");
        }
        HttpEntity entity = this.response.getEntity();
        this.content = entity.getContent();
        return this.content;
    }
    
    public String getResponseEntityAsString() throws Exception
    {
        if(this.response == null)
        {
            throw new Exception("the response is null!!!");
        }

        String responseString = null;
        InputStream responeStream = this.getResponseEntityAsStream();
        byte [] byteArray = new byte[2048];
        int len = responeStream.read(byteArray);
        while(len != -1)
        {
            responseString += new String(byteArray, 0, len);
            len = responeStream.read(byteArray);
        }
        return responseString;
    }
    
    private InputStream content;
    private String url;
    private HttpResponse response;
    private StatusLine statusLine;
    private HttpGet httpGetRequest;
    private DefaultHttpClient httpClient = new DefaultHttpClient();
    
}


public class UrlHandlerTest
{

    /**
     * @param args
     */
    
    
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
//        String url = "http://127.0.0.1/tianyi/";
        String url = "http://127.0.0.1/tianyi/index.php?controller=column&action=require&username=18049229109";
        
        UrlHandler handler = new UrlHandler(url);
        
        try
        {
            HttpResponse response = handler.requstResponse();
            System.out.println(response.toString());
            
            StatusLine statusLine = handler.getStatusLine();
            System.out.println(statusLine);
            
            int statusCode = handler.getStatusCode();
            System.out.println(statusCode);
            
            
            boolean responseOK = handler.isResponseOK();
            System.out.println(responseOK);
            
            String contentType = handler.getContentType();
            System.out.println(contentType);
            
            System.out.println("get response as stream: -------");
            InputStream responseEntity = handler.getResponseEntityAsStream();
            System.out.println(responseEntity.toString());
            System.out.println("get response as stream done----");
            
            System.out.println("\n\n");
            
            System.out.println("get response as string: -------");
            String respString = handler.getResponseEntityAsString();
            System.out.println(respString);
            System.out.println("get response as string done----");
        }
        catch (Exception e) {
            
            // TODO: handle exception
        } finally
        {
        }

    }

}
